package pikasial.images.traitements;

import java.util.LinkedList;
import java.util.ListIterator;

public class TraitementManager {

	private LinkedList<Traitement> commandes;
	private ListIterator<Traitement> listIterator;
	protected Traitement lastTraitement;

	public TraitementManager() {
		commandes = new LinkedList<Traitement>();
		listIterator = commandes.listIterator();
	}

	public void executeTraitement(Traitement t) {
		while (listIterator.hasNext()) {
			listIterator.next();
			listIterator.remove();
		}
		listIterator.add(t);
		lastTraitement = t;
		t.executeTraitement();
	}

	public boolean canUndo() {
		return listIterator.hasPrevious();
	}

	public boolean canRedo() {
		return listIterator.hasNext();
	}


	public void undoTraitement() {
		if (listIterator.hasPrevious()) {
			lastTraitement.undoChanges();
			lastTraitement = listIterator.previous();
		}
	}

	public void redoTraitement() {
		if (listIterator.hasNext()) {
			lastTraitement = listIterator.next();
			lastTraitement.executeTraitement();
		}
	}

	public void clearTraitementList() {
		commandes.clear();
		lastTraitement = null;
	}
}
